package com.home.broadcastreceivers;

import java.util.Calendar;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.home.activities.ResourceActivity;
import com.home.constants.Consts;
import com.home.rhtester.R;

public class NotificationReceiver extends BroadcastReceiver  {
	
	private static int NOTIFICATION_ID=69;



	@Override
	public void onReceive(Context context,Intent intent) {
		Bundle bundle=intent.getExtras();
		
		long notifyBefore=bundle.getLong(Consts.NOTIFY_BEFORE);
		long reservationLegth=bundle.getLong(Consts.RESERVATION_LENGTH)-notifyBefore;
		String raw_data=bundle.getString(Consts.RESOURCE_RAW_DATA);
		
		long notifyBeforeInMin=notifyBefore/(1000*60);
		String resourceName=bundle.getString(Consts.RESOURCE_NAME);
		Calendar cal=Calendar.getInstance();
		long startTime=cal.getTimeInMillis();
		long timePased=0;
		
		
		NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification=new Notification(R.drawable.hardware,"Reservation for resource "+resourceName+" will expire in "+notifyBeforeInMin+" minutes", Calendar.getInstance().getTimeInMillis());
		
		

		CharSequence contentTitle =resourceName+ " Reservation";
		CharSequence contentText = "Will expire in "+notifyBeforeInMin+" minutes";
		Intent notificationIntent = new Intent(context, ResourceActivity.class);
		notificationIntent.putExtra(Consts.RESOURCE_RAW_DATA, raw_data);
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
		//Default notification sound/vibration/lights
		notification.defaults=Notification.DEFAULT_ALL;
		//Auto cancel onClick
		notification.flags|=Notification.FLAG_AUTO_CANCEL;
		
		
		nm.notify(NOTIFICATION_ID, notification);
		
			
	}

}
